//
//  HLBTabBarVC.m
//  honglaba
//
//  Created by 红喇叭 on 2017/6/9.
//  Copyright © 2017年 红喇叭. All rights reserved.
//

#import "HLBTabBarVC.h"
#import "MainViewController.h"
#import "PPHttpRequsetData.h"
#import "BaseNavgationC.h"

#import "HomeViewController.h"
#import "StoreManageVC.h"
#import "NoticeListVCViewController.h"
#import "VersionUpdateView.h"


@interface HLBTabBarVC (){
   
    HomeViewController *onevc;
    StoreManageVC *five;
    NoticeListVCViewController *twovc;
    
}

@end

@implementation HLBTabBarVC

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self setupSubviews];
    [self Checkversion];//检查版本更新
    
}

//检查版本更新
- (void)Checkversion{
    __block NSString *app_Version = [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleShortVersionString"];
    
    
    [[PPHttpRequsetData shareInstance] requestStyle:Request_GET URL:@"/ShopApi/Version/getVsersionInfo?type=ios" parameter:@{} successBlock:^(PPHttpRequest *request, id responseObj) {
    
        if([self judgeNewVersion:responseObj[@"version"] withOldVersion:app_Version]){
            NSLog(@"版本应该更新了");
           //
            NSString *str = responseObj[@"upgradeInfo"];
            NSArray *array = [str componentsSeparatedByString:@"\n"];
            VersionUpdateView* updateV = [[VersionUpdateView alloc] initWithFrame:[UIScreen mainScreen].bounds updateData:array];
            updateV.downloadAdress = responseObj[@"download"];
            [[UIApplication sharedApplication].keyWindow addSubview:updateV];
           
        }else{
            //强制更新
            if([responseObj[@"needUpdate"] intValue] == 1){
                NSString *str = responseObj[@"upgradeInfo"];
                NSArray *array = [str componentsSeparatedByString:@"\n"];
                VersionUpdateView* updateV = [[VersionUpdateView alloc] initWithFrame:[UIScreen mainScreen].bounds updateData:array];
                updateV.downloadAdress = responseObj[@"download"];
                updateV.isMandatory = YES;//强制更新的设置
                [[UIApplication sharedApplication].keyWindow addSubview:updateV];
 
            }
        }

    } failBlock:^(PPHttpRequest *request, NSString *errMsg) {
        
    }];
    

}



- (void)setupSubviews
{
    ///////////////////////////////// ///////////////////////////////// /////////////////////////////////
     onevc = [[HomeViewController alloc] init];
    BaseNavgationC *oneNC = [[BaseNavgationC alloc] initWithRootViewController:onevc];
    onevc.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"首页"
                                                     image:nil
                                                       tag:0];
    [onevc.tabBarItem setFinishedSelectedImage:[[UIImage imageNamed:@"home_tab_icon1__selected.png"]
                                                imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]
                   withFinishedUnselectedImage:[UIImage imageNamed:@"home_tab_icon1__nor.png"]];
    onevc.view.autoresizingMask = UIViewAutoresizingFlexibleHeight;
    [self unSelectedTapTabBarItems:onevc.tabBarItem];
    [self selectedTapTabBarItems:onevc.tabBarItem];
    
    
   twovc = [[NoticeListVCViewController alloc] init];
    BaseNavgationC *twovcNC = [[BaseNavgationC alloc] initWithRootViewController:twovc];
    twovc.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"通知"
                                                     image:nil
                                                       tag:1];
    [twovc.tabBarItem setFinishedSelectedImage:[[UIImage imageNamed:@"home_tab_icon2__selected.png"]
                                                imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]
                   withFinishedUnselectedImage:[UIImage imageNamed:@"home_tab_icon2__nor.png"]];
    twovc.view.autoresizingMask = UIViewAutoresizingFlexibleHeight;
    [self unSelectedTapTabBarItems:twovc.tabBarItem];
    [self selectedTapTabBarItems:twovc.tabBarItem];
    

    
    
    
    five = [[StoreManageVC alloc] init];
    BaseNavgationC *fiveNC = [[BaseNavgationC alloc] initWithRootViewController:five];
    five.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"我的"
                                                    image:nil
                                                      tag:4];
    [five.tabBarItem setFinishedSelectedImage:[[UIImage imageNamed:@"home_tab_icon3__selected.png"]
                                               imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]
                  withFinishedUnselectedImage:[UIImage imageNamed:@"home_tab_icon3__sor.png"]];
    five.view.autoresizingMask = UIViewAutoresizingFlexibleHeight;
    [self unSelectedTapTabBarItems:five.tabBarItem];
    [self selectedTapTabBarItems:five.tabBarItem];
    
    self.viewControllers = @[oneNC,twovcNC,fiveNC];
   

   // [twovc.tabBarItem pp_addDotWithColor:[UIColor orangeColor]];

}



-(void)unSelectedTapTabBarItems:(UITabBarItem *)tabBarItem
{
    [tabBarItem setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:
                                        [UIFont fontWithName:@"Helvetica" size:12.f], UITextAttributeFont,[UIColor grayColor],UITextAttributeTextColor,
                                        nil] forState:UIControlStateNormal];
}

-(void)selectedTapTabBarItems:(UITabBarItem *)tabBarItem
{
    
    [tabBarItem setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:
                                        [UIFont fontWithName:@"Helvetica" size:12.f],
                                        UITextAttributeFont, UIColorFromRGB(0xe71a41),UITextAttributeTextColor,
                                        nil] forState:UIControlStateSelected];
}


#pragma mark - UITabBarDelegate

- (void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item
{
    if([item.title isEqualToString:@"首页"]){
           NSNotification * notice = [NSNotification notificationWithName:@"shouye" object:nil userInfo:nil];
           [[NSNotificationCenter defaultCenter]postNotification:notice];
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

//判断当前app版本和AppStore最新app版本大小
- (BOOL)judgeNewVersion:(NSString *)newVersion withOldVersion:(NSString *)oldVersion
{
    NSArray *newArray = [newVersion componentsSeparatedByCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@"."]];
    NSArray *oldArray = [oldVersion componentsSeparatedByCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@"."]];
    NSInteger count = newArray.count>oldArray.count?oldArray.count:newArray.count;

    for (NSInteger i = 0; i < count; i ++) {
        if ([newArray[i] integerValue] > [oldArray[i] integerValue]) {
            return YES;
        }
    }
    if(newArray.count>oldArray.count) return YES;
    return NO;
}



@end
